//
//  TDUserAccount.swift
//  微博
//
//  Created by tong dan on 16/3/6.
//  Copyright © 2016年 tong dan. All rights reserved.
//

import UIKit

class TDUserAccount: NSObject {

   /*

    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"

   */
    
   //access_token
    var access_token:String?
    // 过期时间,单位是 `秒`
    var expires_in: NSTimeInterval = 0 {
        didSet{
        
        //过期时间等于获取回来的accessToken的那一刻加上expires_in这么多秒
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        
        }
    
    }
    /// 过期时间, date 类型
    var expiresDate: NSDate?
    // 过期时间的字符串类型
    var remind_in:String?
    //用户的在新浪微博的 Id
    var uid:String?
    /// 用户的大图 180*180
    var avatar_large: String?
    /// 用户的昵称
    var screen_name: String?
    
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String)
    {}
    
    override var description: String {
        
        let keys = ["access_token","expires_in","uid","avatar_large","screen_name"]
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
    //归档：告诉系统怎么去保存这个对象（保存哪些属性）
    func encodeWithCoder(encoder:NSCoder){
        encoder.encodeObject(uid,forKey: "uid")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")

        }
    
    //解档:怎么从文档中读取数据(读取哪些数据)
    required init(coder decoder:NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        access_token = decoder.decodeObjectForKey("access_token")as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large")as? String
        screen_name = decoder.decodeObjectForKey("screen_name")as? String
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
}
